﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Finance.IDal;
using Finance.Poco;
using Finance.Entity;
using Dos.ORM;

namespace Finance.Dal
{
    public class BillTypeDal : BaseDal, IBillTypeDal
    {
        public IEnumerable<Entity.AccountsTypeEntity> List(string name, int? isOut, int pageIndex, int pageSize, out int total)
        {
            var sql = dbSession.From<AccountsType>();
            if (!string.IsNullOrWhiteSpace(name))
            {
                sql.Where(at => at.Name.Like(name));
            }
            if (isOut.HasValue)
            {
                sql.Where(at => at.IsOut == isOut);
            }
            total = sql.Count();

            var accountsTypeList = sql.Page(pageSize, pageIndex).ToList();
            return accountsTypeList.Select(AutoMapper.Mapper.Map<AccountsTypeEntity>).ToList();
        }
        public IEnumerable<Entity.AccountsTypeEntity> List()
        {
            var sql = dbSession.From<AccountsType>();
           

            var accountsTypeList = sql.ToList();
            return accountsTypeList.Select(AutoMapper.Mapper.Map<AccountsTypeEntity>).ToList();
        }
        public bool Delete(int id)
        {
            return dbSession.Delete<AccountsType>(id) > 0;
        }

        public bool Save(int id, string name, int isOut)
        {
            AccountsType accountType = new AccountsType();
           
            accountType.Id = id;
            accountType.Name = name;
            accountType.IsOut = isOut;
            if (id == 0)
            {
                accountType.Attach(EntityState.Added);
            }
            else {
                accountType.Attach(EntityState.Modified);
            }
            return dbSession.Save<AccountsType>(accountType) > 0;
        }
    }
}
